# -*- coding: utf-8 -*-
from odoo import models, fields, api, tools, _
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
from odoo.exceptions import ValidationError, Warning
from datetime import timedelta,datetime

class MaintenanceWarning(models.Model):
    """ 
    超期保养
    """
    _name = "tyibs.risk.maintenance.warning"
    _rec_name = "lift_id"

    lift_id = fields.Many2one("tyibs.base.lift", string="电梯", required=True)
    use_address = fields.Char(related="lift_id.use_address")
    use_company_id = fields.Many2one("tyibs.base.lift.use.company", related="lift_id.use_company_id", store=True)
    use_company_contact = fields.Char(string="联系人", size=50 )
    use_company_contact_phone = fields.Char(string="联系人手机")

    maintenance_company_id = fields.Many2one("tyibs.base.maintenance.company", related="lift_id.maintenance_company_id", store=True)
    maintenance_company_contact = fields.Char(string="联系人")
    maintenance_company_contact_phone = fields.Char(string="联系人手机")

    last_check_date = fields.Date(related="lift_id.last_check_date", store=True)
    last_maintenance_date = fields.Date(related="lift_id.last_maintenance_date", store=True)
    next_maintenace_date = fields.Date(string="下次维保时间")

    task_type = fields.Selection([('overtime_main','超期保养'),('overtime_no_main','超期未保养'),('invalid_main','无效保养')], string="任务类型", required=True)

    over_days = fields.Integer(string=u"超期(天)")
    maintenance_state = fields.Selection([('has_main','已保养'),('no_main','未保养'),('invalid_main','无效维保')], string=u"维保状态")
    state = fields.Selection([('report_warning','发起报警'),('doing','正在处理'),('has_done','处理完成'),('archive','归档')],string="状态", default="report_warning")
    
    qs_company_id = fields.Many2one("tyibs.base.qs.company", string=u"整改单位")
    qs_employee_id = fields.Many2one("tyibs.base.qs.company.employee", string=u"整改人员", domain="[('enterprise_id','=',qs_company_id)]")
    notice_method = fields.Selection([('system','系统通知'),('message','消息通知')], string=u"通知方式")
    notice_content = fields.Text(string="通知内容")
    comment_ids = fields.One2many("tyibs.risk.maintenance.warning.comment", "warning_id", string="处理记录")
    remark = fields.Text(string="备注")

    @api.onchange("last_maintenance_date")
    def _change_next_maintenance_date(self):
        """
        计算下次应该维保的时间
        """
        self.ensure_one()
        if self.last_maintenance_date:
            maintenance_date = datetime.strptime(self.last_maintenance_date, DEFAULT_SERVER_DATE_FORMAT)
            today = datetime.strptime(fields.Date.today(), DEFAULT_SERVER_DATE_FORMAT);
            self.next_maintenace_date = maintenance_date + timedelta(days=15)
            self.over_days = (today - maintenance_date).days

    @api.onchange("use_company_id")
    def _change_use_company(self):
        self.ensure_one()
        if self.use_company_id:
            self.use_company_contact = self.use_company_id.linkman
            self.use_company_contact_phone = self.use_company_id.linkman_phone

    @api.onchange("maintenance_company_id")
    def _change_maintenance_company(self):
        self.ensure_one()
        if self.maintenance_company_id:
            self.maintenance_company_contact = self.maintenance_company_id.contact
            self.maintenance_company_contact_phone = self.maintenance_company_id.mobile

    @api.model
    def search(self, args, offset=0, limit=None, order=None, count=False):
        qs_company_id = self.env.user.qs_company_id
        if qs_company_id and qs_company_id.level == '2':
            args.append(('lift_id.area_id', '=', qs_company_id.area_id.id))
        return super(MaintenanceWarning, self).search(args, offset, limit, order, count)


class MaintenanceWarningComment(models.Model):
    """
    维保警报日志
    """
    _name = "tyibs.risk.maintenance.warning.comment"
    _rec_name = "do_person"

    warning_id = fields.Many2one("tyibs.risk.maintenance.warning", string="所属警报", required=True)
    do_person = fields.Char(string="处理人", size=100, required=True)
    do_phone = fields.Char(string="处理电话", size=30)
    do_company = fields.Char(string="处理单位", size=300)
    do_time = fields.Datetime(string="处理时间")
    do_content = fields.Text(string="处理内容")
    operator_user_id = fields.Many2one("res.users", string="处理人", default=lambda self:self.env.uid)
